package com.zhongxi.guoyuan.adapter.shopCart;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.entity.shopcart.AddressListData;
import com.zhongxi.guoyuan.entity.shopcart.AddressListResult;
import com.zhongxi.guoyuan.view.rewardCoin.RewardAddressListActivity;
import com.zhongxi.guoyuan.view.rewardCoin.RewardEditAddressActivity;

import java.util.List;

import static android.R.attr.data;

/**
 * Created by Administrator on 2017/2/21.
 */

public class AddressListAdapter extends BaseAdapter {

    private Context context;
    private List<AddressListData> list;
    private Handler handler;
    private int selectPos = -1;

    public AddressListAdapter(Context context, List<AddressListData> list, Handler handler) {
        this.context = context;
        this.list = list;
        this.handler = handler;
    }

    public int getSelectPos() {
        return selectPos;
    }

    public void setSelectPos(int selectPos) {
        this.selectPos = selectPos;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        try {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_address_list, parent, false);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                holder.tvPhone = (TextView) convertView.findViewById(R.id.tvPhone);
                holder.tvAddress = (TextView) convertView.findViewById(R.id.tvAddress);
                holder.relaDelete = (RelativeLayout) convertView.findViewById(R.id.relaDelete);
                holder.relaEdit = (RelativeLayout) convertView.findViewById(R.id.relaEdit);
                holder.relaSelect = (RelativeLayout) convertView.findViewById(R.id.relaSelect);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            final AddressListData data = list.get(position);
            if (null != data.getUsername() && !"".equals(data.getUsername())) {
                holder.tvName.setText("收货人:" + data.getUsername());
            } else {
                holder.tvName.setText("收货人：");
            }
            if (null != data.getMphone() && !"".equals(data.getMphone())) {
                holder.tvPhone.setText("电话:" + data.getMphone());
            } else {
                holder.tvPhone.setText("电话:");
            }
            if (null != data.getAddress() && !"".equals(data.getAddress())) {
                holder.tvAddress.setText("地址:" + data.getProvince() + data.getCity() + data.getPrefecture() + data.getAddress());
            } else {
                holder.tvAddress.setText("地址:");
            }
            holder.relaDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Message msg = Message.obtain();
                    msg.what = position;
                    handler.sendMessage(msg);
                }
            });

            holder.relaEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = null;
                    intent = new Intent(context, RewardEditAddressActivity.class);
                    intent.putExtra("data", data);
                    ((Activity) context).startActivityForResult(intent, RewardAddressListActivity.REQUESTADDRESSCODE);
                }
            });

            if ("1".equals(data.getIsdefault())) {
                selectPos = position;
                data.setIsdefault("0");
            }

            if (position == selectPos) {
                holder.ivImg.setImageResource(R.mipmap.yes);
            } else {
                holder.ivImg.setImageResource(R.mipmap.no);
            }
            if (selectPos != -1) {
                MyApplication.getInstance().sharedPrefrences.edit().putString("rewardName", list.get(selectPos).getUsername()).commit();
                MyApplication.getInstance().sharedPrefrences.edit().putString("rewardAddressid", list.get(selectPos).getId()).commit();
                MyApplication.getInstance().sharedPrefrences.edit().putString("rewardUserphone", list.get(selectPos).getMphone()).commit();
                MyApplication.getInstance().sharedPrefrences.edit().putString("rewardAddress", list.get(selectPos).getProvince()
                        + list.get(selectPos).getCity() + list.get(selectPos).getPrefecture() + list.get(selectPos).getAddress()).commit();
            }

            holder.relaSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (selectPos != position) {
                        selectPos = position;
                    } else {
                        selectPos = -1;
                    }
                    notifyDataSetChanged();
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder {
        private TextView tvName;
        private TextView tvPhone;
        private TextView tvAddress;
        private ImageView ivImg;
        private RelativeLayout relaDelete;
        private RelativeLayout relaEdit;
        private RelativeLayout relaSelect;
    }
}
